package com.prestamomo.vil.ppa.pmvilpage.pmfitter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.prestamomo.vil.ppa.PmvilApp;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmInitLoan;
import com.prestamomo.vil.ppa.pmvilapi.PmvilCheckPage;

import java.util.List;
import java.util.Random;

public class PmvilListingFiter extends RecyclerView.Adapter<PmvilListingFiter.PmvilListingHolde> {
    private Context pmvilCont;
    private List<PmInitLoan.ApplyInfosBean> pmvilListProduct;

    private String[] pmvilListingStr = {"99% 成功率","高金額","無審核"};
    public PmvilListingFiter(Context pmvilCont, List<PmInitLoan.ApplyInfosBean> pmvilListProduct) {
        this.pmvilCont = pmvilCont;
        this.pmvilListProduct = pmvilListProduct;
    }

    @NonNull
    @Override
    public PmvilListingFiter.PmvilListingHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PmvilListingHolde(LayoutInflater.from(pmvilCont).inflate(R.layout.listing_pmvil_item_ly,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull PmvilListingFiter.PmvilListingHolde holder, int position) {
        holder.listingPmvilFiterTvtype.setText(pmvilListingStr[new Random().nextInt(2)]);
        holder.listingPmvilFiterIvvip.setVisibility(pmvilListProduct.get(position).getDoubleLoan()?View.VISIBLE:View.GONE);
        holder.listingPmvilFiterTvtype.setVisibility(pmvilListProduct.get(position).getDoubleLoan()?View.VISIBLE:View.GONE);

        Glide.with(pmvilCont).load(pmvilListProduct.get(position).getProductIcon()).placeholder(R.mipmap.logo_pm_icon).into(holder.listingPmvilFiterIvicon);
        holder.listingPmvilFiterTvnam.setText(pmvilListProduct.get(position).getProductName());
        holder.listingPmvilFiterTvmoney.setText("$"+pmvilListProduct.get(position).getLoanAmount());
        holder.pmlset_liste_se.setText("$"+pmvilListProduct.get(position).getReviewAmount());
        holder.listing_pmvil_tv_hkrq.setText(pmvilListProduct.get(position).getRepayDate());
        holder.listing_pmvil_tv_lv.setText(pmvilListProduct.get(position).getLowestInterestRate());
        holder.listing_pmvil_tv_kbf.setText("$"+pmvilListProduct.get(position).getServiceCharge());


        holder.listingPmvilFiterLlsq.setOnClickListener(v -> {
            if(pmvilListingInterface!=null){
                pmvilListingInterface.setPmvilListingProdu(pmvilListProduct.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return pmvilListProduct.size();
    }

    public class PmvilListingHolde extends RecyclerView.ViewHolder{
        private  TextView listingPmvilFiterTvnam;
        private  TextView listingPmvilFiterTvtype;
        private  TextView listingPmvilFiterTvmoney;
        private  TextView pmlset_liste_se;
        private  TextView listing_pmvil_tv_hkrq;
        private  TextView listing_pmvil_tv_lv;
        private  TextView listing_pmvil_tv_kbf;
        private  LinearLayout listingPmvilFiterLlsq;
        private ImageView listingPmvilFiterIvicon;
        private ImageView listingPmvilFiterIvvip;
        public PmvilListingHolde(@NonNull View itemView) {
            super(itemView);
            initPmvilListingEcted(itemView, PmvilApp.getPmvilApss());
        }

        private void initPmvilListingEcted(@NonNull View itemView,Context pmvilCOnts) {
            pmlset_liste_se = itemView.findViewById(R.id.pmlset_liste_se);
            listingPmvilFiterIvicon = itemView.findViewById(R.id.listing_pmvil_item_iv_icon);
            listingPmvilFiterIvvip = itemView.findViewById(R.id.listing_pmvil_item_iv_vip);
            listing_pmvil_tv_kbf = itemView.findViewById(R.id.listing_pmvil_tv_kbf);
            listing_pmvil_tv_lv = itemView.findViewById(R.id.listing_pmvil_tv_lv);
            listing_pmvil_tv_hkrq = itemView.findViewById(R.id.listing_pmvil_tv_hkrq);
            initPmvilLIsw(itemView);
            initPmvilLiswse(itemView);
        }

        private void initPmvilLiswse(@NonNull View itemView) {
            listingPmvilFiterTvmoney = itemView.findViewById(R.id.listing_pmvil_item_tv_money);
            listingPmvilFiterLlsq = itemView.findViewById(R.id.listing_pmvil_item_ll_sq);
        }

        private void initPmvilLIsw(@NonNull View itemView) {
            listingPmvilFiterTvnam = itemView.findViewById(R.id.listing_pmvil_item_tv_name);
            listingPmvilFiterTvtype = itemView.findViewById(R.id.listing_pmvil_item_tv_type);
        }
    }

    public interface PmvilListingInterface{
        void setPmvilListingProdu(PmInitLoan.ApplyInfosBean pmvilListingBena);
    }

    private PmvilListingInterface pmvilListingInterface;

    public void setPmvilListingInterface(PmvilListingInterface pmvilListingInterface) {
        this.pmvilListingInterface = pmvilListingInterface;
    }
}
